--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

--
-- ROLES
--
CREATE ROLE "MulaToR__plugin_admin" NOLOGIN;
CREATE ROLE "MulaToR__plugin_user" NOLOGIN;
CREATE ROLE "MulaToR__private_admin" NOLOGIN;
CREATE ROLE "MulaToR__server_admin" NOLOGIN;
CREATE ROLE "MulaToR__server_user" NOLOGIN;
CREATE ROLE "MulaToR__snap_admin" NOLOGIN;
CREATE ROLE "MulaToR__snap_user" NOLOGIN;

--
-- Name: plugin; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA plugin;
ALTER SCHEMA plugin OWNER TO postgres;

--
-- Name: SCHEMA plugin; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA plugin IS 'WIP: This will be the space for the plugins names and keys. Also libraries could be stored here, they are functions on any language that are able to parse command outputs and give all the util data as rows.';

--
-- Name: private; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA private;
ALTER SCHEMA private OWNER TO postgres;

--
-- Name: SCHEMA private; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA private IS 'WIP: This is a private space to use internal only. User and password list could be saved here.';

--
-- Name: report; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA report;
ALTER SCHEMA report OWNER TO postgres;

--
-- Name: SCHEMA report; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA report IS 'This is space is designed for custom tables, here you can keep useful information that is updated and used by plugins.';

--
-- Name: server; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA server;
ALTER SCHEMA server OWNER TO postgres;

--
-- Name: SCHEMA server; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA server IS 'This is the base information for your environment.
Here goes the almost static data like os version, etc that shoud be writen once but used a lot of times.';

--
-- Name: snap; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA snap;
ALTER SCHEMA snap OWNER TO postgres;

--
-- Name: SCHEMA snap; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA snap IS 'Here goes al the data captured from the servers.
One snap represents multiple output from a server. These output are referenced to a key based on the command executed and stored on snap_data.';

--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: 
--
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;

--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: 
--
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
SET search_path = plugin, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;

--
-- Name: plugin; Type: TABLE; Schema: plugin; Owner: postgres; Tablespace: 
--
CREATE TABLE plugin (
    id integer NOT NULL,
    name character varying(64) NOT NULL
);


ALTER TABLE plugin.plugin OWNER TO postgres;

--
-- Name: list_plugin(character varying); Type: FUNCTION; Schema: plugin; Owner: postgres
--
CREATE FUNCTION list_plugin(v_name character varying) RETURNS SETOF plugin
    LANGUAGE plpgsql
    AS $$DECLARE
 v_plugin plugin.plugin%ROWTYPE;
BEGIN
 FOR v_plugin IN SELECT * FROM plugin.plugin WHERE plugin.name LIKE v_name LOOP
  RETURN NEXT v_plugin;
 END LOOP;
 RETURN;
END;$$;


ALTER FUNCTION plugin.list_plugin(v_name character varying) OWNER TO postgres;

SET search_path = private, pg_catalog;
--
-- Name: add_admin(integer, character varying, character varying); Type: FUNCTION; Schema: private; Owner: postgres
--
CREATE FUNCTION add_admin(v_server_id integer, v_name character varying, v_pass character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
BEGIN
 INSERT INTO private.admin (server_id, name, pass) VALUES (v_server_id, v_name, v_pass);
 RETURN v_server_id;
END;$$;

ALTER FUNCTION private.add_admin(v_server_id integer, v_name character varying, v_pass character varying) OWNER TO postgres;

--
-- Name: add_login(integer, character varying, character varying); Type: FUNCTION; Schema: private; Owner: postgres
--
CREATE FUNCTION add_login(v_server_id integer, v_name character varying, v_pass character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
BEGIN
 INSERT INTO private.login (server_id, name, pass) VALUES (v_server_id, v_name, v_pass);
 RETURN v_server_id;
END;$$;

ALTER FUNCTION private.add_login(v_server_id integer, v_name character varying, v_pass character varying) OWNER TO postgres;

--
-- Name: admin; Type: TABLE; Schema: private; Owner: postgres; Tablespace: 
--
CREATE TABLE admin (
    server_id integer NOT NULL,
    name character varying(256),
    pass character varying(256)
);

ALTER TABLE private.admin OWNER TO postgres;

--
-- Name: get_admin(integer); Type: FUNCTION; Schema: private; Owner: postgres
--
CREATE FUNCTION get_admin(v_server_id integer) RETURNS admin
    LANGUAGE plpgsql
    AS $$DECLARE
 v_admin private.admin%ROWTYPE;
BEGIN
 SELECT * INTO v_admin FROM private.admin WHERE admin.server_id = v_server_id;
 RETURN v_admin;
END;$$;

ALTER FUNCTION private.get_admin(v_server_id integer) OWNER TO postgres;

--
-- Name: login; Type: TABLE; Schema: private; Owner: postgres; Tablespace: 
--
CREATE TABLE login (
    server_id integer,
    name character varying(64),
    pass character varying(256)
);

ALTER TABLE private.login OWNER TO postgres;

--
-- Name: get_login(integer); Type: FUNCTION; Schema: private; Owner: postgres
--
CREATE FUNCTION get_login(v_server_id integer) RETURNS login
    LANGUAGE plpgsql
    AS $$DECLARE
 v_login private.login%ROWTYPE;
BEGIN
 SELECT * INTO v_login FROM private.login WHERE login.server_id = v_server_id;
 RETURN v_login;
END;$$;

ALTER FUNCTION private.get_login(v_server_id integer) OWNER TO postgres;

--
-- Name: up_admin(integer, character varying, character varying); Type: FUNCTION; Schema: private; Owner: postgres
--
CREATE FUNCTION up_admin(v_server_id integer, v_name character varying, v_pass character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
BEGIN
 UPDATE private.admin SET name = v_name, pass = v_pass WHERE server_id = v_server_id;
 RETURN v_server_id;
END;$$;

ALTER FUNCTION private.up_admin(v_server_id integer, v_name character varying, v_pass character varying) OWNER TO postgres;

SET search_path = server, pg_catalog;
--
-- Name: add_arch_name(character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION add_arch_name(v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 INSERT INTO server.arch_name (name) VALUES (v_name);
 SELECT currval('server.seq_arch_name_id') INTO v_id;
 RETURN v_id;
END;$$;


ALTER FUNCTION server.add_arch_name(v_name character varying) OWNER TO postgres;

--
-- Name: add_arch_type(integer, character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION add_arch_type(v_arch_name_id integer, v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 INSERT INTO server.arch_type (arch_name_id, name) VALUES (v_arch_name_id, v_name);
 SELECT currval('server.seq_arch_type_id') INTO v_id;
 RETURN v_id;
END;$$;


ALTER FUNCTION server.add_arch_type(v_arch_name_id integer, v_name character varying) OWNER TO postgres;

--
-- Name: add_os_name(character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION add_os_name(v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 INSERT INTO server.os_name (name) VALUES (v_name);
 SELECT currval('server.seq_os_name_id') INTO v_id;
 RETURN v_id;
END;$$;


ALTER FUNCTION server.add_os_name(v_name character varying) OWNER TO postgres;

--
-- Name: add_os_version(integer, character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION add_os_version(v_os_name_id integer, v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 INSERT INTO server.os_version (os_name_id, name) VALUES (v_os_name_id, v_name);
 SELECT currval('server.seq_os_version_id') INTO v_id;
 RETURN v_id;
END;$$;

ALTER FUNCTION server.add_os_version(v_os_name_id integer, v_name character varying) OWNER TO postgres;

--
-- Name: add_server(character varying, character varying, integer, integer); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION add_server(v_name character varying, v_ip character varying, v_arch_type_id integer, v_os_version_id integer) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 INSERT INTO server.server (name, ip, arch_type_id, os_version_id) VALUES (v_name, v_ip, v_arch_type_id, v_os_version_id);
 SELECT currval('server.seq_server_id') INTO v_id;
 RETURN v_id;
END;$$;

ALTER FUNCTION server.add_server(v_name character varying, v_ip character varying, v_arch_type_id integer, v_os_version_id integer) OWNER TO postgres;

--
-- Name: get_arch_name_id(character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION get_arch_name_id(v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 SELECT id INTO v_id FROM server.arch_name WHERE arch_name.name LIKE v_name;
 IF FOUND
  THEN
   RETURN v_id;
  ELSE
   RETURN NULL;
 END IF;
END;$$;

ALTER FUNCTION server.get_arch_name_id(v_name character varying) OWNER TO postgres;

--
-- Name: get_arch_type_id(integer, character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION get_arch_type_id(v_arch_name_id integer, v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 SELECT id INTO v_id FROM server.arch_type WHERE arch_type.arch_name_id = v_arch_name_id AND arch_type.name LIKE v_name;
 IF FOUND
  THEN
   RETURN v_id;
  ELSE
   RETURN NULL;
 END IF;
END;$$;

ALTER FUNCTION server.get_arch_type_id(v_arch_name_id integer, v_name character varying) OWNER TO postgres;

--
-- Name: get_os_name_id(character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION get_os_name_id(v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 SELECT id INTO v_id FROM server.os_name WHERE os_name.name LIKE v_name;
 IF FOUND
  THEN
   RETURN v_id;
  ELSE
   RETURN NULL;
 END IF;
END;$$;

ALTER FUNCTION server.get_os_name_id(v_name character varying) OWNER TO postgres;

--
-- Name: get_os_version_id(integer, character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION get_os_version_id(v_os_name_id integer, v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 SELECT id INTO v_id FROM server.os_version WHERE os_version.os_name_id = v_os_name_id AND os_version.name LIKE v_name;
 IF FOUND
  THEN
   RETURN v_id;
  ELSE
   RETURN NULL;
 END IF;
END;$$;

ALTER FUNCTION server.get_os_version_id(v_os_name_id integer, v_name character varying) OWNER TO postgres;

--
-- Name: get_server_full(integer); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION get_server_full(v_server_id integer) RETURNS record
    LANGUAGE plpgsql
    AS $$DECLARE
 v_server_all RECORD;
 v_server_name VARCHAR;
 v_server_ip VARCHAR;
 v_os_name VARCHAR;
 v_os_version VARCHAR;
BEGIN
 SELECT server.id, server.name, server.ip, os_name.name, os_version.name INTO  v_server_name, v_server_ip, v_os_name, v_os_version FROM server.server, server.os_name, server.os_version, server.arch_name, server.arch_type WHERE server.id = v_server_id;
 RETURN v_server_all;
END;$$;

ALTER FUNCTION server.get_server_full(v_server_id integer) OWNER TO postgres;

--
-- Name: get_server_id(character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION get_server_id(v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 SELECT id INTO v_id FROM server.server WHERE server.name = v_name;
 IF FOUND
  THEN
   RETURN v_id;
  ELSE
   RETURN NULL;
 END IF;
END;$$;

ALTER FUNCTION server.get_server_id(v_name character varying) OWNER TO postgres;

--
-- Name: get_server_text(integer); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION get_server_text(v_server_id integer, OUT v_server_name character varying, OUT v_server_ip character varying, OUT v_os_name character varying, OUT v_os_version character varying, OUT v_arch_name character varying, OUT v_arch_type character varying) RETURNS record
    LANGUAGE plpgsql
    AS $$DECLARE
BEGIN
 SELECT server.name, server.ip, os_name.name, os_version.name, arch_name.name, arch_type.name
 INTO  v_server_name, v_server_ip, v_os_name, v_os_version, v_arch_name, v_arch_type
 FROM server.server JOIN server.os_version ON (server.os_version_id = os_version.id)
 JOIN server.os_name ON (os_version.os_name_id = os_name.id)
 JOIN server.arch_type ON (server.arch_type_id = arch_type.id)
 JOIN server.arch_name ON (arch_type.arch_name_id = arch_name.id)
 WHERE server.id = v_server_id;
 RETURN;
END;$$;

ALTER FUNCTION server.get_server_text(v_server_id integer, OUT v_server_name character varying, OUT v_server_ip character varying, OUT v_os_name character varying, OUT v_os_version character varying, OUT v_arch_name character varying, OUT v_arch_type character varying) OWNER TO postgres;

--
-- Name: seq_server_id; Type: SEQUENCE; Schema: server; Owner: postgres
--
CREATE SEQUENCE seq_server_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE server.seq_server_id OWNER TO postgres;

--
-- Name: seq_server_id; Type: SEQUENCE SET; Schema: server; Owner: postgres
--
-- SELECT pg_catalog.setval('seq_server_id', 1, true);

--
-- Name: server; Type: TABLE; Schema: server; Owner: postgres; Tablespace: 
--
CREATE TABLE server (
    id integer DEFAULT nextval('seq_server_id'::regclass) NOT NULL,
    name character varying(64) NOT NULL,
    ip character varying(16) NOT NULL,
    arch_type_id integer NOT NULL,
    os_version_id integer NOT NULL,
    info text
);

ALTER TABLE server.server OWNER TO postgres;

--
-- Name: TABLE server; Type: COMMENT; Schema: server; Owner: postgres
--
COMMENT ON TABLE server IS 'This is the main table, a list of servers with an unique id, name and ip. It is also stored the architecture type and os version to help filtering them.';

--
-- Name: list_server(character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION list_server(v_name character varying) RETURNS SETOF server
    LANGUAGE plpgsql
    AS $$DECLARE
 v_server server.server%ROWTYPE;
BEGIN
 FOR v_server IN SELECT * FROM server.server WHERE server.name LIKE v_name LOOP
  RETURN NEXT v_server;
 END LOOP;
 RETURN;
END;$$;

ALTER FUNCTION server.list_server(v_name character varying) OWNER TO postgres;

--
-- Name: list_server_text(character varying); Type: FUNCTION; Schema: server; Owner: postgres
--
CREATE FUNCTION list_server_text(v_name character varying, OUT v_server_id integer, OUT v_server_name character varying, OUT v_server_ip character varying, OUT v_os_name character varying, OUT v_os_version character varying, OUT v_arch_name character varying, OUT v_arch_type character varying) RETURNS SETOF record
    LANGUAGE plpgsql
    AS $$
DECLARE
BEGIN
 FOR v_server_id, v_server_name, v_server_ip, v_os_name, v_os_version, v_arch_name, v_arch_type
 IN SELECT server.id, server.name, server.ip, os_name.name, os_version.name, arch_name.name, arch_type.name
 FROM server.server JOIN server.os_version ON (server.os_version_id = os_version.id)
 JOIN server.os_name ON (os_version.os_name_id = os_name.id)
 JOIN server.arch_type ON (server.arch_type_id = arch_type.id)
 JOIN server.arch_name ON (arch_type.arch_name_id = arch_name.id)
 WHERE server.name LIKE v_name
 LOOP
  RETURN NEXT;
 END LOOP;
 RETURN;
END;$$;

ALTER FUNCTION server.list_server_text(v_name character varying, OUT v_server_id integer, OUT v_server_name character varying, OUT v_server_ip character varying, OUT v_os_name character varying, OUT v_os_version character varying, OUT v_arch_name character varying, OUT v_arch_type character varying) OWNER TO postgres;

SET search_path = snap, pg_catalog;
--
-- Name: add_snap(integer, character varying, character varying); Type: FUNCTION; Schema: snap; Owner: postgres
--
CREATE FUNCTION add_snap(v_server_id integer, v_date character varying, v_login character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 SELECT id INTO v_id FROM snap.snap WHERE snap.server_id = v_server_id AND snap.date LIKE v_date;
 IF FOUND
  THEN
   RAISE NOTICE 'snap duplicated (%)', v_id;
   RETURN NULL;
  ELSE
   INSERT INTO snap.snap (server_id, date, login) VALUES (v_server_id, v_date, v_login);
   SELECT currval('snap.seq_snap_id') INTO v_id;
   RETURN v_id;
 END IF;
END;$$;


ALTER FUNCTION snap.add_snap(v_server_id integer, v_date character varying, v_login character varying) OWNER TO postgres;

--
-- Name: add_snap_data(integer, integer, text); Type: FUNCTION; Schema: snap; Owner: postgres
--
CREATE FUNCTION add_snap_data(v_snap_id integer, v_snap_key_id integer, v_data text) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 INSERT INTO snap.snap_data (snap_id, snap_key_id, data) VALUES (v_snap_id, v_snap_key_id, v_data);
 SELECT currval('snap.seq_snap_data_id') INTO v_id;
 RETURN v_id;
END;$$;

ALTER FUNCTION snap.add_snap_data(v_snap_id integer, v_snap_key_id integer, v_data text) OWNER TO postgres;

--
-- Name: add_snap_key(character varying); Type: FUNCTION; Schema: snap; Owner: postgres
--
CREATE FUNCTION add_snap_key(v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 INSERT INTO snap.snap_key (name) VALUES (v_name);
 SELECT currval('snap.seq_snap_key_id') INTO v_id;
 RETURN v_id;
END;$$;

ALTER FUNCTION snap.add_snap_key(v_name character varying) OWNER TO postgres;

--
-- Name: get_snap(integer, character varying); Type: FUNCTION; Schema: snap; Owner: postgres
--
CREATE FUNCTION get_snap(v_server_id integer, v_date character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 SELECT id INTO v_id FROM snap.snap WHERE snap.server_id = v_server_id AND snap.date LIKE v_date;
 IF FOUND
  THEN
   RETURN v_id;
  ELSE
   RETURN NULL;
 END IF;
END;$$;

ALTER FUNCTION snap.get_snap(v_server_id integer, v_date character varying) OWNER TO postgres;

--
-- Name: get_snap_info(integer); Type: FUNCTION; Schema: snap; Owner: postgres
--
CREATE FUNCTION get_snap_info(v_snap_id integer, OUT v_snap_id_out integer, OUT v_snap_timestamp character varying, OUT v_server_name character varying, OUT v_os_name_id integer, OUT v_os_name character varying, OUT v_os_version_id integer, OUT v_os_version character varying, OUT v_server_ip character varying) RETURNS record
    LANGUAGE plpgsql
    AS $$
DECLARE
BEGIN
 SELECT snap.id, snap.timestamp, server.name, os_name.id, os_name.name, os_version.id, os_version.name, server.ip
 INTO v_snap_id_out, v_snap_timestamp, v_server_name, v_os_name_id, v_os_name, v_os_version_id, v_os_version, v_server_ip 
 FROM snap.snap JOIN server.server ON (snap.server_id = server.id)
 JOIN server.os_version ON (server.os_version_id = os_version.id)
 JOIN server.os_name ON (os_version.os_name_id = os_name.id)
 WHERE snap.id = v_snap_id;
 RETURN;
END;
$$;

ALTER FUNCTION snap.get_snap_info(v_snap_id integer, OUT v_snap_id_out integer, OUT v_snap_timestamp character varying, OUT v_server_name character varying, OUT v_os_name_id integer, OUT v_os_name character varying, OUT v_os_version_id integer, OUT v_os_version character varying, OUT v_server_ip character varying) OWNER TO postgres;

--
-- Name: get_snap_key_id(character varying); Type: FUNCTION; Schema: snap; Owner: postgres
--

CREATE FUNCTION get_snap_key_id(v_name character varying) RETURNS integer
    LANGUAGE plpgsql
    AS $$DECLARE
 v_id integer;
BEGIN
 SELECT id INTO v_id FROM snap.snap_key WHERE snap_key.name LIKE v_name;
 IF FOUND
  THEN
   RETURN v_id;
  ELSE
   RETURN NULL;
 END IF;
END;$$;

ALTER FUNCTION snap.get_snap_key_id(v_name character varying) OWNER TO postgres;

--
-- Name: get_snap_match(integer, character varying[]); Type: FUNCTION; Schema: snap; Owner: postgres
--
CREATE FUNCTION get_snap_match(v_server_id integer, v_snap_key_name character varying[], OUT v_snap_id integer, OUT v_snap_timestamp character varying, OUT v_count integer) RETURNS record
    LANGUAGE plpgsql
    AS $$DECLARE
BEGIN
 SELECT snap.id, snap.timestamp, COUNT(*)
  INTO v_snap_id, v_snap_timestamp, v_count
  FROM snap.snap_data JOIN snap.snap_key ON (snap_key.id = snap_data.snap_key_id)
  JOIN snap.snap ON (snap_data.snap_id = snap.id)
  WHERE snap_key.name = ANY (v_snap_key_name)
  AND snap.server_id = v_server_id
  GROUP BY snap.id
  ORDER BY COUNT(*) DESC, snap.timestamp DESC
  LIMIT 1;
RETURN;
END;$$;


ALTER FUNCTION snap.get_snap_match(v_server_id integer, v_snap_key_name character varying[], OUT v_snap_id integer, OUT v_snap_timestamp character varying, OUT v_count integer) OWNER TO postgres;

--
-- Name: get_snap_text(integer); Type: FUNCTION; Schema: snap; Owner: postgres
--
CREATE FUNCTION get_snap_text(v_snap_id integer, OUT v_snap_key character varying, OUT v_snap_data text) RETURNS SETOF record
    LANGUAGE plpgsql
    AS $$DECLARE
BEGIN
 FOR v_snap_key, v_snap_data 
 IN SELECT snap_key.name, snap_data.data
 FROM snap.snap_key JOIN snap.snap_data
 ON (snap_key.id = snap_data.snap_key_id)
 WHERE snap_data.snap_id = v_snap_id
 LOOP
  RETURN NEXT;
 END LOOP;
 RETURN;
END;$$;

ALTER FUNCTION snap.get_snap_text(v_snap_id integer, OUT v_snap_key character varying, OUT v_snap_data text) OWNER TO postgres;

--
-- Name: list_snap_server(integer); Type: FUNCTION; Schema: snap; Owner: postgres
--
CREATE FUNCTION list_snap_server(v_server_id integer, OUT v_snap_id integer, OUT v_snap_timestamp character varying, OUT v_snap_date character varying, OUT v_snap_login character varying) RETURNS SETOF record
    LANGUAGE plpgsql
    AS $$DECLARE
BEGIN
 FOR v_snap_id, v_snap_timestamp, v_snap_date, v_snap_login
 IN SELECT id, timestamp, date, login
 FROM snap.snap
 WHERE snap.server_id = v_server_id
 LOOP
  RETURN NEXT;
 END LOOP;
 RETURN;
END;$$;

ALTER FUNCTION snap.list_snap_server(v_server_id integer, OUT v_snap_id integer, OUT v_snap_timestamp character varying, OUT v_snap_date character varying, OUT v_snap_login character varying) OWNER TO postgres;

SET search_path = plugin, pg_catalog;
--
-- Name: plugin_key; Type: TABLE; Schema: plugin; Owner: postgres; Tablespace: 
--
CREATE TABLE plugin_key (
    id integer NOT NULL,
    plugin_id integer NOT NULL,
    snap_key_id integer NOT NULL
);

ALTER TABLE plugin.plugin_key OWNER TO postgres;

--
-- Name: seq_plugin_id; Type: SEQUENCE; Schema: plugin; Owner: postgres
--
CREATE SEQUENCE seq_plugin_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE plugin.seq_plugin_id OWNER TO postgres;

--
-- Name: seq_plugin_id; Type: SEQUENCE OWNED BY; Schema: plugin; Owner: postgres
--
ALTER SEQUENCE seq_plugin_id OWNED BY plugin.id;

--
-- Name: seq_plugin_id; Type: SEQUENCE SET; Schema: plugin; Owner: postgres
--
-- SELECT pg_catalog.setval('seq_plugin_id', 1, true);

--
-- Name: seq_plugin_key_id; Type: SEQUENCE; Schema: plugin; Owner: postgres
--
CREATE SEQUENCE seq_plugin_key_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE plugin.seq_plugin_key_id OWNER TO postgres;

--
-- Name: seq_plugin_key_id; Type: SEQUENCE OWNED BY; Schema: plugin; Owner: postgres
--
ALTER SEQUENCE seq_plugin_key_id OWNED BY plugin_key.id;

--
-- Name: seq_plugin_key_id; Type: SEQUENCE SET; Schema: plugin; Owner: postgres
--
-- SELECT pg_catalog.setval('seq_plugin_key_id', 1, false);

SET search_path = report, pg_catalog;
--
-- Name: csv; Type: TABLE; Schema: report; Owner: postgres; Tablespace: 
--
CREATE TABLE csv (
    id integer NOT NULL,
    a character varying,
    b character varying,
    c character varying,
    d character varying,
    e character varying,
    f character varying,
    g character varying,
    h character varying,
    i character varying,
    j character varying,
    k character varying,
    l character varying,
    m character varying,
    n character varying,
    o character varying,
    p character varying,
    q character varying,
    r character varying,
    s character varying,
    t character varying,
    u character varying,
    v character varying,
    w character varying,
    x character varying,
    y character varying,
    z character varying,
    server_id integer
);


ALTER TABLE report.csv OWNER TO postgres;

--
-- Name: csv_id_seq; Type: SEQUENCE; Schema: report; Owner: postgres
--
CREATE SEQUENCE csv_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE report.csv_id_seq OWNER TO postgres;

--
-- Name: csv_id_seq; Type: SEQUENCE OWNED BY; Schema: report; Owner: postgres
--
ALTER SEQUENCE csv_id_seq OWNED BY csv.id;


--
-- Name: csv_id_seq; Type: SEQUENCE SET; Schema: report; Owner: postgres
--
-- SELECT pg_catalog.setval('csv_id_seq', 1, true);

SET search_path = server, pg_catalog;
--
-- Name: seq_arch_name_id; Type: SEQUENCE; Schema: server; Owner: postgres
--
CREATE SEQUENCE seq_arch_name_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE server.seq_arch_name_id OWNER TO postgres;

--
-- Name: seq_arch_name_id; Type: SEQUENCE SET; Schema: server; Owner: postgres
--
-- SELECT pg_catalog.setval('seq_arch_name_id', 1, true);

--
-- Name: arch_name; Type: TABLE; Schema: server; Owner: postgres; Tablespace: 
--
CREATE TABLE arch_name (
    id integer DEFAULT nextval('seq_arch_name_id'::regclass) NOT NULL,
    name character varying(64) NOT NULL,
    info text
);


ALTER TABLE server.arch_name OWNER TO postgres;

--
-- Name: TABLE arch_name; Type: COMMENT; Schema: server; Owner: postgres
--
COMMENT ON TABLE arch_name IS 'This table is a list of known hardware architecture.';

--
-- Name: seq_arch_type_id; Type: SEQUENCE; Schema: server; Owner: postgres
--
CREATE SEQUENCE seq_arch_type_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE server.seq_arch_type_id OWNER TO postgres;

--
-- Name: seq_arch_type_id; Type: SEQUENCE SET; Schema: server; Owner: postgres
--
-- SELECT pg_catalog.setval('seq_arch_type_id', 1, true);

--
-- Name: arch_type; Type: TABLE; Schema: server; Owner: postgres; Tablespace: 
--
CREATE TABLE arch_type (
    id integer DEFAULT nextval('seq_arch_type_id'::regclass) NOT NULL,
    arch_name_id integer NOT NULL,
    name character varying NOT NULL,
    info text
);

ALTER TABLE server.arch_type OWNER TO postgres;

--
-- Name: TABLE arch_type; Type: COMMENT; Schema: server; Owner: postgres
--
COMMENT ON TABLE arch_type IS 'This table keeps the relation between architecture name and type.';

--
-- Name: seq_os_name_id; Type: SEQUENCE; Schema: server; Owner: postgres
--
CREATE SEQUENCE seq_os_name_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE server.seq_os_name_id OWNER TO postgres;

--
-- Name: seq_os_name_id; Type: SEQUENCE SET; Schema: server; Owner: postgres
--
-- SELECT pg_catalog.setval('seq_os_name_id', 1, true);

--
-- Name: os_name; Type: TABLE; Schema: server; Owner: postgres; Tablespace: 
--
CREATE TABLE os_name (
    id integer DEFAULT nextval('seq_os_name_id'::regclass) NOT NULL,
    name character varying(64) NOT NULL,
    info text
);

ALTER TABLE server.os_name OWNER TO postgres;

--
-- Name: TABLE os_name; Type: COMMENT; Schema: server; Owner: postgres
--
COMMENT ON TABLE os_name IS 'This table is a list of know operative systems.';

--
-- Name: seq_os_version_id; Type: SEQUENCE; Schema: server; Owner: postgres
--
CREATE SEQUENCE seq_os_version_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE server.seq_os_version_id OWNER TO postgres;

--
-- Name: seq_os_version_id; Type: SEQUENCE SET; Schema: server; Owner: postgres
--
-- SELECT pg_catalog.setval('seq_os_version_id', 1, true);

--
-- Name: os_version; Type: TABLE; Schema: server; Owner: postgres; Tablespace: 
--
CREATE TABLE os_version (
    id integer DEFAULT nextval('seq_os_version_id'::regclass) NOT NULL,
    os_name_id integer,
    name character varying(64) NOT NULL,
    info text
);


ALTER TABLE server.os_version OWNER TO postgres;

--
-- Name: TABLE os_version; Type: COMMENT; Schema: server; Owner: postgres
--
COMMENT ON TABLE os_version IS 'This table keeps the relation between OS name and version.';

SET search_path = snap, pg_catalog;
--
-- Name: seq_snap_data_id; Type: SEQUENCE; Schema: snap; Owner: postgres
--
CREATE SEQUENCE seq_snap_data_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE snap.seq_snap_data_id OWNER TO postgres;

--
-- Name: seq_snap_data_id; Type: SEQUENCE SET; Schema: snap; Owner: postgres
--
-- SELECT pg_catalog.setval('seq_snap_data_id', 1, true);

--
-- Name: seq_snap_id; Type: SEQUENCE; Schema: snap; Owner: postgres
--
CREATE SEQUENCE seq_snap_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE snap.seq_snap_id OWNER TO postgres;

--
-- Name: seq_snap_id; Type: SEQUENCE SET; Schema: snap; Owner: postgres
--
-- SELECT pg_catalog.setval('seq_snap_id', 1, true);

--
-- Name: seq_snap_key_id; Type: SEQUENCE; Schema: snap; Owner: postgres
--
CREATE SEQUENCE seq_snap_key_id
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE snap.seq_snap_key_id OWNER TO postgres;

--
-- Name: seq_snap_key_id; Type: SEQUENCE SET; Schema: snap; Owner: postgres
--
-- SELECT pg_catalog.setval('seq_snap_key_id', 1, true);

--
-- Name: snap; Type: TABLE; Schema: snap; Owner: postgres; Tablespace: 
--
CREATE TABLE snap (
    id integer DEFAULT nextval('seq_snap_id'::regclass) NOT NULL,
    "timestamp" timestamp without time zone DEFAULT now() NOT NULL,
    server_id integer NOT NULL,
    date character varying(32),
    login character varying(64)
);


ALTER TABLE snap.snap OWNER TO postgres;

--
-- Name: TABLE snap; Type: COMMENT; Schema: snap; Owner: postgres
--
COMMENT ON TABLE snap IS 'This table will keep track of dates and relation between server and commands output. It is the main index for snapshots.';

--
-- Name: snap_data; Type: TABLE; Schema: snap; Owner: postgres; Tablespace: 
--
CREATE TABLE snap_data (
    id integer DEFAULT nextval('seq_snap_data_id'::regclass) NOT NULL,
    snap_id integer NOT NULL,
    snap_key_id integer NOT NULL,
    data text
);

ALTER TABLE snap.snap_data OWNER TO postgres;

--
-- Name: TABLE snap_data; Type: COMMENT; Schema: snap; Owner: postgres
--
COMMENT ON TABLE snap_data IS 'This table stores the commands output and is the one using more space.';

--
-- Name: snap_key; Type: TABLE; Schema: snap; Owner: postgres; Tablespace: 
--
CREATE TABLE snap_key (
    id integer DEFAULT nextval('seq_snap_key_id'::regclass) NOT NULL,
    name character varying(256) NOT NULL
);


ALTER TABLE snap.snap_key OWNER TO postgres;

--
-- Name: TABLE snap_key; Type: COMMENT; Schema: snap; Owner: postgres
--
COMMENT ON TABLE snap_key IS 'This table keeps a relation between the varchar command line used and an unique int id assigned to save space.';

SET search_path = plugin, pg_catalog;
--
-- Name: id; Type: DEFAULT; Schema: plugin; Owner: postgres
--
ALTER TABLE ONLY plugin ALTER COLUMN id SET DEFAULT nextval('seq_plugin_id'::regclass);

--
-- Name: id; Type: DEFAULT; Schema: plugin; Owner: postgres
--
ALTER TABLE ONLY plugin_key ALTER COLUMN id SET DEFAULT nextval('seq_plugin_key_id'::regclass);


SET search_path = report, pg_catalog;
--
-- Name: id; Type: DEFAULT; Schema: report; Owner: postgres
--
ALTER TABLE ONLY csv ALTER COLUMN id SET DEFAULT nextval('csv_id_seq'::regclass);


SET search_path = plugin, pg_catalog;
--
-- Data for Name: plugin; Type: TABLE DATA; Schema: plugin; Owner: postgres
--

--
-- Data for Name: plugin_key; Type: TABLE DATA; Schema: plugin; Owner: postgres
--

SET search_path = private, pg_catalog;
--
-- Data for Name: admin; Type: TABLE DATA; Schema: private; Owner: postgres
--

--
-- Data for Name: login; Type: TABLE DATA; Schema: private; Owner: postgres
--

SET search_path = report, pg_catalog;
--
-- Data for Name: csv; Type: TABLE DATA; Schema: report; Owner: postgres
--

SET search_path = server, pg_catalog;
--
-- Data for Name: arch_name; Type: TABLE DATA; Schema: server; Owner: postgres
--

--
-- Data for Name: arch_type; Type: TABLE DATA; Schema: server; Owner: postgres
--

--
-- Data for Name: os_name; Type: TABLE DATA; Schema: server; Owner: postgres
--

--
-- Data for Name: os_version; Type: TABLE DATA; Schema: server; Owner: postgres
--

--
-- Data for Name: server; Type: TABLE DATA; Schema: server; Owner: postgres
--

SET search_path = snap, pg_catalog;
--
-- Data for Name: snap; Type: TABLE DATA; Schema: snap; Owner: postgres
--

--
-- Data for Name: snap_data; Type: TABLE DATA; Schema: snap; Owner: postgres
--

--
-- Data for Name: snap_key; Type: TABLE DATA; Schema: snap; Owner: postgres
--

SET search_path = plugin, pg_catalog;
--
-- Name: plugin_key_pkey; Type: CONSTRAINT; Schema: plugin; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY plugin_key
    ADD CONSTRAINT plugin_key_pkey PRIMARY KEY (id);

--
-- Name: plugin_name_key; Type: CONSTRAINT; Schema: plugin; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY plugin
    ADD CONSTRAINT plugin_name_key UNIQUE (name);

--
-- Name: plugin_pkey; Type: CONSTRAINT; Schema: plugin; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY plugin
    ADD CONSTRAINT plugin_pkey PRIMARY KEY (id);

--
-- Name: plugin_snap_key; Type: CONSTRAINT; Schema: plugin; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY plugin_key
    ADD CONSTRAINT plugin_snap_key UNIQUE (plugin_id, snap_key_id);

SET search_path = private, pg_catalog;
--
-- Name: admin_server_id; Type: CONSTRAINT; Schema: private; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY admin
    ADD CONSTRAINT admin_server_id UNIQUE (server_id);

--
-- Name: server_id; Type: CONSTRAINT; Schema: private; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY login
    ADD CONSTRAINT server_id UNIQUE (server_id);


SET search_path = report, pg_catalog;
--
-- Name: csv_id_pk; Type: CONSTRAINT; Schema: report; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY csv
    ADD CONSTRAINT csv_id_pk PRIMARY KEY (id);

SET search_path = server, pg_catalog;
--
-- Name: PK; Type: CONSTRAINT; Schema: server; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY server
    ADD CONSTRAINT "PK" PRIMARY KEY (id);

--
-- Name: arch_name_name_key; Type: CONSTRAINT; Schema: server; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY arch_name
    ADD CONSTRAINT arch_name_name_key UNIQUE (name);

--
-- Name: arch_name_pkey; Type: CONSTRAINT; Schema: server; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY arch_name
    ADD CONSTRAINT arch_name_pkey PRIMARY KEY (id);

--
-- Name: arch_type_name; Type: CONSTRAINT; Schema: server; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY arch_type
    ADD CONSTRAINT arch_type_name UNIQUE (arch_name_id, name);


--
-- Name: arch_type_pkey; Type: CONSTRAINT; Schema: server; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY arch_type
    ADD CONSTRAINT arch_type_pkey PRIMARY KEY (id);

--
-- Name: name_ip; Type: CONSTRAINT; Schema: server; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY server
    ADD CONSTRAINT name_ip UNIQUE (name, ip);

--
-- Name: os_name_name_key; Type: CONSTRAINT; Schema: server; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY os_name
    ADD CONSTRAINT os_name_name_key UNIQUE (name);

--
-- Name: os_name_pkey; Type: CONSTRAINT; Schema: server; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY os_name
    ADD CONSTRAINT os_name_pkey PRIMARY KEY (id);

--
-- Name: os_version_name_key; Type: CONSTRAINT; Schema: server; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY os_version
    ADD CONSTRAINT os_version_name_key UNIQUE (name);

--
-- Name: os_version_pkey; Type: CONSTRAINT; Schema: server; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY os_version
    ADD CONSTRAINT os_version_pkey PRIMARY KEY (id);

SET search_path = snap, pg_catalog;
--
-- Name: snap_data_id; Type: CONSTRAINT; Schema: snap; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY snap_data
    ADD CONSTRAINT snap_data_id PRIMARY KEY (id);

--
-- Name: snap_key_name_key; Type: CONSTRAINT; Schema: snap; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY snap_key
    ADD CONSTRAINT snap_key_name_key UNIQUE (name);

--
-- Name: snap_key_pkey; Type: CONSTRAINT; Schema: snap; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY snap_key
    ADD CONSTRAINT snap_key_pkey PRIMARY KEY (id);

--
-- Name: snap_pkey; Type: CONSTRAINT; Schema: snap; Owner: postgres; Tablespace: 
--
ALTER TABLE ONLY snap
    ADD CONSTRAINT snap_pkey PRIMARY KEY (id);

--
-- Name: snap_server_id_key; Type: CONSTRAINT; Schema: snap; Owner: postgres; Tablespace: 
--

ALTER TABLE ONLY snap
    ADD CONSTRAINT snap_server_id_key UNIQUE (server_id, date);


SET search_path = private, pg_catalog;
--
-- Name: fki_server; Type: INDEX; Schema: private; Owner: postgres; Tablespace: 
--
CREATE INDEX fki_server ON admin USING btree (server_id);

--
-- Name: fki_server_id; Type: INDEX; Schema: private; Owner: postgres; Tablespace: 
--
CREATE INDEX fki_server_id ON login USING btree (server_id);

SET search_path = report, pg_catalog;
--
-- Name: csv_server_idx; Type: INDEX; Schema: report; Owner: postgres; Tablespace: 
--
CREATE INDEX csv_server_idx ON csv USING btree (server_id);

SET search_path = snap, pg_catalog;
--
-- Name: snap_id; Type: INDEX; Schema: snap; Owner: postgres; Tablespace: 
--
CREATE INDEX snap_id ON snap_data USING btree (snap_id);

SET search_path = plugin, pg_catalog;
--
-- Name: plugin_id; Type: FK CONSTRAINT; Schema: plugin; Owner: postgres
--
ALTER TABLE ONLY plugin_key
    ADD CONSTRAINT plugin_id FOREIGN KEY (plugin_id) REFERENCES plugin(id) MATCH FULL ON UPDATE RESTRICT ON DELETE RESTRICT;

--
-- Name: snap_key_id; Type: FK CONSTRAINT; Schema: plugin; Owner: postgres
--
ALTER TABLE ONLY plugin_key
    ADD CONSTRAINT snap_key_id FOREIGN KEY (snap_key_id) REFERENCES snap.snap_key(id) MATCH FULL ON UPDATE RESTRICT ON DELETE RESTRICT;

SET search_path = private, pg_catalog;
--
-- Name: server_id; Type: FK CONSTRAINT; Schema: private; Owner: postgres
--
ALTER TABLE ONLY admin
    ADD CONSTRAINT server_id FOREIGN KEY (server_id) REFERENCES server.server(id) MATCH FULL ON UPDATE RESTRICT ON DELETE RESTRICT;

SET search_path = report, pg_catalog;
--
-- Name: csv_server_id; Type: FK CONSTRAINT; Schema: report; Owner: postgres
--
ALTER TABLE ONLY csv
    ADD CONSTRAINT csv_server_id FOREIGN KEY (server_id) REFERENCES server.server(id) MATCH FULL ON UPDATE RESTRICT ON DELETE RESTRICT;

SET search_path = server, pg_catalog;
--
-- Name: arch_name_id; Type: FK CONSTRAINT; Schema: server; Owner: postgres
--
ALTER TABLE ONLY arch_type
    ADD CONSTRAINT arch_name_id FOREIGN KEY (arch_name_id) REFERENCES arch_name(id) MATCH FULL ON UPDATE RESTRICT ON DELETE RESTRICT;

--
-- Name: arch_type_id; Type: FK CONSTRAINT; Schema: server; Owner: postgres
--
ALTER TABLE ONLY server
    ADD CONSTRAINT arch_type_id FOREIGN KEY (arch_type_id) REFERENCES arch_type(id) MATCH FULL ON UPDATE RESTRICT ON DELETE RESTRICT;

--
-- Name: os_name_id; Type: FK CONSTRAINT; Schema: server; Owner: postgres
--
ALTER TABLE ONLY os_version
    ADD CONSTRAINT os_name_id FOREIGN KEY (os_name_id) REFERENCES os_name(id) MATCH FULL ON UPDATE RESTRICT ON DELETE RESTRICT;

--
-- Name: os_version_id; Type: FK CONSTRAINT; Schema: server; Owner: postgres
--
ALTER TABLE ONLY server
    ADD CONSTRAINT os_version_id FOREIGN KEY (os_version_id) REFERENCES os_version(id) MATCH FULL ON UPDATE RESTRICT ON DELETE RESTRICT;

SET search_path = snap, pg_catalog;
--
-- Name: snap_id; Type: FK CONSTRAINT; Schema: snap; Owner: postgres
--
ALTER TABLE ONLY snap_data
    ADD CONSTRAINT snap_id FOREIGN KEY (snap_id) REFERENCES snap(id) MATCH FULL ON UPDATE RESTRICT ON DELETE RESTRICT;

--
-- Name: snap_key_id; Type: FK CONSTRAINT; Schema: snap; Owner: postgres
--
ALTER TABLE ONLY snap_data
    ADD CONSTRAINT snap_key_id FOREIGN KEY (snap_key_id) REFERENCES snap_key(id) MATCH FULL ON UPDATE RESTRICT ON DELETE RESTRICT;

--
-- Name: plugin; Type: ACL; Schema: -; Owner: postgres
--
REVOKE ALL ON SCHEMA plugin FROM PUBLIC;
GRANT USAGE ON SCHEMA plugin TO "MulaToR__plugin_user";

--
-- Name: private; Type: ACL; Schema: -; Owner: postgres
--
REVOKE ALL ON SCHEMA private FROM PUBLIC;
GRANT USAGE ON SCHEMA private TO "MulaToR__private_admin";

--
-- Name: report; Type: ACL; Schema: -; Owner: postgres
--
REVOKE ALL ON SCHEMA report FROM PUBLIC;
GRANT USAGE ON SCHEMA report TO "MulaToR__server_user";

--
-- Name: server; Type: ACL; Schema: -; Owner: postgres
--
REVOKE ALL ON SCHEMA server FROM PUBLIC;
GRANT USAGE ON SCHEMA server TO "MulaToR__server_user";

--
-- Name: snap; Type: ACL; Schema: -; Owner: postgres
--
REVOKE ALL ON SCHEMA snap FROM PUBLIC;
GRANT USAGE ON SCHEMA snap TO "MulaToR__snap_user";


SET search_path = plugin, pg_catalog;
--
-- Name: plugin; Type: ACL; Schema: plugin; Owner: postgres
--
REVOKE ALL ON TABLE plugin FROM PUBLIC;
GRANT SELECT ON TABLE plugin TO "MulaToR__plugin_user";


SET search_path = private, pg_catalog;
--
-- Name: add_admin(integer, character varying, character varying); Type: ACL; Schema: private; Owner: postgres
--
REVOKE ALL ON FUNCTION add_admin(v_server_id integer, v_name character varying, v_pass character varying) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION add_admin(v_server_id integer, v_name character varying, v_pass character varying) TO "MulaToR__private_admin";

--
-- Name: add_login(integer, character varying, character varying); Type: ACL; Schema: private; Owner: postgres
--
REVOKE ALL ON FUNCTION add_login(v_server_id integer, v_name character varying, v_pass character varying) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION add_login(v_server_id integer, v_name character varying, v_pass character varying) TO "MulaToR__private_admin";

--
-- Name: admin; Type: ACL; Schema: private; Owner: postgres
--
REVOKE ALL ON TABLE admin FROM PUBLIC;
GRANT SELECT,INSERT,UPDATE ON TABLE admin TO "MulaToR__private_admin";

--
-- Name: get_admin(integer); Type: ACL; Schema: private; Owner: postgres
--
REVOKE ALL ON FUNCTION get_admin(v_server_id integer) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION get_admin(v_server_id integer) TO "MulaToR__private_admin";

--
-- Name: login; Type: ACL; Schema: private; Owner: postgres
--
REVOKE ALL ON TABLE login FROM PUBLIC;
GRANT SELECT,INSERT,UPDATE ON TABLE login TO "MulaToR__private_admin";

--
-- Name: get_login(integer); Type: ACL; Schema: private; Owner: postgres
--
REVOKE ALL ON FUNCTION get_login(v_server_id integer) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION get_login(v_server_id integer) TO "MulaToR__private_admin";


--
-- Name: up_admin(integer, character varying, character varying); Type: ACL; Schema: private; Owner: postgres
--
REVOKE ALL ON FUNCTION up_admin(v_server_id integer, v_name character varying, v_pass character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION up_admin(v_server_id integer, v_name character varying, v_pass character varying) FROM postgres;
GRANT EXECUTE ON FUNCTION up_admin(v_server_id integer, v_name character varying, v_pass character varying) TO "MulaToR__private_admin";

SET search_path = server, pg_catalog;
--
-- Name: add_arch_name(character varying); Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON FUNCTION add_arch_name(v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION add_arch_name(v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION add_arch_name(v_name character varying) TO postgres;
GRANT ALL ON FUNCTION add_arch_name(v_name character varying) TO PUBLIC;
GRANT ALL ON FUNCTION add_arch_name(v_name character varying) TO "MulaToR__server_admin";


--
-- Name: add_arch_type(integer, character varying); Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON FUNCTION add_arch_type(v_arch_name_id integer, v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION add_arch_type(v_arch_name_id integer, v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION add_arch_type(v_arch_name_id integer, v_name character varying) TO postgres;
GRANT ALL ON FUNCTION add_arch_type(v_arch_name_id integer, v_name character varying) TO PUBLIC;
GRANT ALL ON FUNCTION add_arch_type(v_arch_name_id integer, v_name character varying) TO "MulaToR__server_admin";


--
-- Name: add_os_name(character varying); Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON FUNCTION add_os_name(v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION add_os_name(v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION add_os_name(v_name character varying) TO postgres;
GRANT ALL ON FUNCTION add_os_name(v_name character varying) TO PUBLIC;
GRANT ALL ON FUNCTION add_os_name(v_name character varying) TO "MulaToR__server_admin";


--
-- Name: add_os_version(integer, character varying); Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON FUNCTION add_os_version(v_os_name_id integer, v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION add_os_version(v_os_name_id integer, v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION add_os_version(v_os_name_id integer, v_name character varying) TO postgres;
GRANT ALL ON FUNCTION add_os_version(v_os_name_id integer, v_name character varying) TO PUBLIC;
GRANT ALL ON FUNCTION add_os_version(v_os_name_id integer, v_name character varying) TO "MulaToR__server_admin";


--
-- Name: add_server(character varying, character varying, integer, integer); Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON FUNCTION add_server(v_name character varying, v_ip character varying, v_arch_type_id integer, v_os_version_id integer) FROM PUBLIC;
REVOKE ALL ON FUNCTION add_server(v_name character varying, v_ip character varying, v_arch_type_id integer, v_os_version_id integer) FROM postgres;
GRANT ALL ON FUNCTION add_server(v_name character varying, v_ip character varying, v_arch_type_id integer, v_os_version_id integer) TO postgres;
GRANT ALL ON FUNCTION add_server(v_name character varying, v_ip character varying, v_arch_type_id integer, v_os_version_id integer) TO "MulaToR__server_admin";


--
-- Name: get_arch_name_id(character varying); Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON FUNCTION get_arch_name_id(v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION get_arch_name_id(v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION get_arch_name_id(v_name character varying) TO postgres;
GRANT ALL ON FUNCTION get_arch_name_id(v_name character varying) TO "MulaToR__server_user";


--
-- Name: get_arch_type_id(integer, character varying); Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON FUNCTION get_arch_type_id(v_arch_name_id integer, v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION get_arch_type_id(v_arch_name_id integer, v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION get_arch_type_id(v_arch_name_id integer, v_name character varying) TO postgres;
GRANT ALL ON FUNCTION get_arch_type_id(v_arch_name_id integer, v_name character varying) TO "MulaToR__server_user";


--
-- Name: get_os_name_id(character varying); Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON FUNCTION get_os_name_id(v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION get_os_name_id(v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION get_os_name_id(v_name character varying) TO postgres;
GRANT ALL ON FUNCTION get_os_name_id(v_name character varying) TO PUBLIC;
GRANT ALL ON FUNCTION get_os_name_id(v_name character varying) TO "MulaToR__server_user";


--
-- Name: get_os_version_id(integer, character varying); Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON FUNCTION get_os_version_id(v_os_name_id integer, v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION get_os_version_id(v_os_name_id integer, v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION get_os_version_id(v_os_name_id integer, v_name character varying) TO postgres;
GRANT ALL ON FUNCTION get_os_version_id(v_os_name_id integer, v_name character varying) TO PUBLIC;
GRANT ALL ON FUNCTION get_os_version_id(v_os_name_id integer, v_name character varying) TO "MulaToR__server_user";


--
-- Name: get_server_id(character varying); Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON FUNCTION get_server_id(v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION get_server_id(v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION get_server_id(v_name character varying) TO postgres;
GRANT ALL ON FUNCTION get_server_id(v_name character varying) TO PUBLIC;
GRANT ALL ON FUNCTION get_server_id(v_name character varying) TO "MulaToR__server_user";


--
-- Name: seq_server_id; Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON SEQUENCE seq_server_id FROM PUBLIC;
REVOKE ALL ON SEQUENCE seq_server_id FROM postgres;
GRANT ALL ON SEQUENCE seq_server_id TO postgres;
GRANT SELECT,UPDATE ON SEQUENCE seq_server_id TO "MulaToR__server_admin";


--
-- Name: server; Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON TABLE server FROM PUBLIC;
REVOKE ALL ON TABLE server FROM postgres;
GRANT ALL ON TABLE server TO postgres;
GRANT SELECT ON TABLE server TO "MulaToR__server_user";
GRANT SELECT,INSERT ON TABLE server TO "MulaToR__server_admin";


SET search_path = snap, pg_catalog;

--
-- Name: add_snap(integer, character varying, character varying); Type: ACL; Schema: snap; Owner: postgres
--

REVOKE ALL ON FUNCTION add_snap(v_server_id integer, v_date character varying, v_login character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION add_snap(v_server_id integer, v_date character varying, v_login character varying) FROM postgres;
GRANT ALL ON FUNCTION add_snap(v_server_id integer, v_date character varying, v_login character varying) TO postgres;
GRANT ALL ON FUNCTION add_snap(v_server_id integer, v_date character varying, v_login character varying) TO PUBLIC;
GRANT ALL ON FUNCTION add_snap(v_server_id integer, v_date character varying, v_login character varying) TO "MulaToR__snap_admin";


--
-- Name: add_snap_data(integer, integer, text); Type: ACL; Schema: snap; Owner: postgres
--

REVOKE ALL ON FUNCTION add_snap_data(v_snap_id integer, v_snap_key_id integer, v_data text) FROM PUBLIC;
REVOKE ALL ON FUNCTION add_snap_data(v_snap_id integer, v_snap_key_id integer, v_data text) FROM postgres;
GRANT ALL ON FUNCTION add_snap_data(v_snap_id integer, v_snap_key_id integer, v_data text) TO postgres;
GRANT ALL ON FUNCTION add_snap_data(v_snap_id integer, v_snap_key_id integer, v_data text) TO PUBLIC;
GRANT ALL ON FUNCTION add_snap_data(v_snap_id integer, v_snap_key_id integer, v_data text) TO "MulaToR__snap_admin";


--
-- Name: add_snap_key(character varying); Type: ACL; Schema: snap; Owner: postgres
--

REVOKE ALL ON FUNCTION add_snap_key(v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION add_snap_key(v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION add_snap_key(v_name character varying) TO postgres;
GRANT ALL ON FUNCTION add_snap_key(v_name character varying) TO PUBLIC;
GRANT ALL ON FUNCTION add_snap_key(v_name character varying) TO "MulaToR__snap_admin";


--
-- Name: get_snap_key_id(character varying); Type: ACL; Schema: snap; Owner: postgres
--

REVOKE ALL ON FUNCTION get_snap_key_id(v_name character varying) FROM PUBLIC;
REVOKE ALL ON FUNCTION get_snap_key_id(v_name character varying) FROM postgres;
GRANT ALL ON FUNCTION get_snap_key_id(v_name character varying) TO postgres;
GRANT ALL ON FUNCTION get_snap_key_id(v_name character varying) TO PUBLIC;
GRANT ALL ON FUNCTION get_snap_key_id(v_name character varying) TO "MulaToR__snap_admin";


SET search_path = plugin, pg_catalog;

--
-- Name: plugin_key; Type: ACL; Schema: plugin; Owner: postgres
--

REVOKE ALL ON TABLE plugin_key FROM PUBLIC;
REVOKE ALL ON TABLE plugin_key FROM postgres;
GRANT ALL ON TABLE plugin_key TO postgres;
GRANT SELECT ON TABLE plugin_key TO "MulaToR__plugin_user";


SET search_path = server, pg_catalog;

--
-- Name: seq_arch_name_id; Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON SEQUENCE seq_arch_name_id FROM PUBLIC;
REVOKE ALL ON SEQUENCE seq_arch_name_id FROM postgres;
GRANT ALL ON SEQUENCE seq_arch_name_id TO postgres;
GRANT SELECT,UPDATE ON SEQUENCE seq_arch_name_id TO "MulaToR__server_admin";


--
-- Name: arch_name; Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON TABLE arch_name FROM PUBLIC;
REVOKE ALL ON TABLE arch_name FROM postgres;
GRANT ALL ON TABLE arch_name TO postgres;
GRANT SELECT,INSERT ON TABLE arch_name TO "MulaToR__server_admin";
GRANT SELECT ON TABLE arch_name TO "MulaToR__server_user";


--
-- Name: seq_arch_type_id; Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON SEQUENCE seq_arch_type_id FROM PUBLIC;
REVOKE ALL ON SEQUENCE seq_arch_type_id FROM postgres;
GRANT ALL ON SEQUENCE seq_arch_type_id TO postgres;
GRANT SELECT,UPDATE ON SEQUENCE seq_arch_type_id TO "MulaToR__server_admin";


--
-- Name: arch_type; Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON TABLE arch_type FROM PUBLIC;
REVOKE ALL ON TABLE arch_type FROM postgres;
GRANT ALL ON TABLE arch_type TO postgres;
GRANT SELECT,INSERT ON TABLE arch_type TO "MulaToR__server_admin";
GRANT SELECT ON TABLE arch_type TO "MulaToR__server_user";


--
-- Name: seq_os_name_id; Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON SEQUENCE seq_os_name_id FROM PUBLIC;
REVOKE ALL ON SEQUENCE seq_os_name_id FROM postgres;
GRANT ALL ON SEQUENCE seq_os_name_id TO postgres;
GRANT SELECT,UPDATE ON SEQUENCE seq_os_name_id TO "MulaToR__server_admin";


--
-- Name: os_name; Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON TABLE os_name FROM PUBLIC;
REVOKE ALL ON TABLE os_name FROM postgres;
GRANT ALL ON TABLE os_name TO postgres;
GRANT SELECT ON TABLE os_name TO "MulaToR__server_user";
GRANT SELECT,INSERT ON TABLE os_name TO "MulaToR__server_admin";


--
-- Name: seq_os_version_id; Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON SEQUENCE seq_os_version_id FROM PUBLIC;
REVOKE ALL ON SEQUENCE seq_os_version_id FROM postgres;
GRANT ALL ON SEQUENCE seq_os_version_id TO postgres;
GRANT SELECT,UPDATE ON SEQUENCE seq_os_version_id TO "MulaToR__server_admin";


--
-- Name: os_version; Type: ACL; Schema: server; Owner: postgres
--

REVOKE ALL ON TABLE os_version FROM PUBLIC;
REVOKE ALL ON TABLE os_version FROM postgres;
GRANT ALL ON TABLE os_version TO postgres;
GRANT SELECT ON TABLE os_version TO "MulaToR__server_user";
GRANT SELECT,INSERT ON TABLE os_version TO "MulaToR__server_admin";


SET search_path = snap, pg_catalog;

--
-- Name: seq_snap_data_id; Type: ACL; Schema: snap; Owner: postgres
--

REVOKE ALL ON SEQUENCE seq_snap_data_id FROM PUBLIC;
REVOKE ALL ON SEQUENCE seq_snap_data_id FROM postgres;
GRANT ALL ON SEQUENCE seq_snap_data_id TO postgres;
GRANT SELECT,UPDATE ON SEQUENCE seq_snap_data_id TO "MulaToR__snap_admin";
GRANT SELECT ON SEQUENCE seq_snap_data_id TO "MulaToR__snap_user";


--
-- Name: seq_snap_id; Type: ACL; Schema: snap; Owner: postgres
--

REVOKE ALL ON SEQUENCE seq_snap_id FROM PUBLIC;
REVOKE ALL ON SEQUENCE seq_snap_id FROM postgres;
GRANT ALL ON SEQUENCE seq_snap_id TO postgres;
GRANT SELECT,UPDATE ON SEQUENCE seq_snap_id TO "MulaToR__snap_admin";


--
-- Name: seq_snap_key_id; Type: ACL; Schema: snap; Owner: postgres
--

REVOKE ALL ON SEQUENCE seq_snap_key_id FROM PUBLIC;
REVOKE ALL ON SEQUENCE seq_snap_key_id FROM postgres;
GRANT ALL ON SEQUENCE seq_snap_key_id TO postgres;
GRANT SELECT,UPDATE ON SEQUENCE seq_snap_key_id TO "MulaToR__snap_admin";


--
-- Name: snap; Type: ACL; Schema: snap; Owner: postgres
--

REVOKE ALL ON TABLE snap FROM PUBLIC;
REVOKE ALL ON TABLE snap FROM postgres;
GRANT ALL ON TABLE snap TO postgres;
GRANT SELECT,INSERT ON TABLE snap TO "MulaToR__snap_admin";


--
-- Name: snap_data; Type: ACL; Schema: snap; Owner: postgres
--

REVOKE ALL ON TABLE snap_data FROM PUBLIC;
REVOKE ALL ON TABLE snap_data FROM postgres;
GRANT ALL ON TABLE snap_data TO postgres;
GRANT SELECT,INSERT ON TABLE snap_data TO "MulaToR__snap_admin";


--
-- Name: snap_key; Type: ACL; Schema: snap; Owner: postgres
--

REVOKE ALL ON TABLE snap_key FROM PUBLIC;
REVOKE ALL ON TABLE snap_key FROM postgres;
GRANT ALL ON TABLE snap_key TO postgres;
GRANT SELECT,INSERT ON TABLE snap_key TO "MulaToR__snap_admin";


--
-- PostgreSQL database dump complete
--

